package com.example.expandablelistview.multimedia;

import android.app.Activity;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Toast;

import com.example.expandablelistview.R;

public class MPTest extends Activity {
	MediaPlayer mPlayer;
	String mSdPath;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mul_mptest);
		
		mPlayer = MediaPlayer.create(this, R.raw.dingdong);
		mSdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
		
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		if (mPlayer != null	){
			mPlayer.release();
			mPlayer = null;
		}
	}
	
	public void mOnClick(View v){
		MediaPlayer player;
		
		switch (v.getId()) {
		case R.id.btn1://resource재생
			player = MediaPlayer.create(this, R.raw.dingdong);
			player.start();
			break;
		case R.id.btn2://file 재생
			player = new MediaPlayer();
			try {
				player.setDataSource(mSdPath + "/oldpop.mp3");
				player.prepare();
				player.start();
			} catch (Exception e) {
				Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
			}
			break;
		case R.id.btn3://스트림재생
			player = new MediaPlayer();
			Uri uri =  Uri.parse("http://www.soen.kr/data/saemaul1.mp3");
			try {
				player.setDataSource(this, uri);
				player.prepare();
			} catch (Exception e){
				Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
			}
			player.start();
			break;
		case R.id.btn4://미리 준비된 객체로 생성
			mPlayer.seekTo(0);
			mPlayer.start();
			break;
		case R.id.btn5://준비하지않은(prepare()호출 하지 않음) 재생
			player = new MediaPlayer();
			try {
				player.setDataSource(mSdPath+"oldpop.mp3");
				//player.prepare();
				player.start();
			}  catch (Exception e) {
				Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
			}
			break;
		case R.id.btn6:// 다른 파일 열기
			player = MediaPlayer.create(this, R.raw.dingdong);
			//player.reset();
			try {
				player.setDataSource(mSdPath + "oldpop.mp3");
				player.prepare();
				player.start();
			} catch (Exception e) {
				Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
			}
			break;
		}
	}
}
